using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using MIMPGameLibrary;

namespace MIMPWinForms.SpriteDisplays
{
    /// <summary>
    /// Extends SpriteDisplay to hold a reference to a Hex object associated with the SpriteDisplay.
    /// </summary>
    public class HexDisplay : SpriteDisplay
    {
        private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        /// <summary>
        /// A partially transparent black hexagon, used to draw a "shadow" on top of the hex being hovered over.
        /// </summary>
        Texture2D hexShadow;
        /// <summary>
        /// A hexagon that is entirely transparent except for its outline.
        /// </summary>
        Texture2D hexOutline;


        private Hex hex;
        /// <summary>
        /// A reference to the Hex object being displayed.
        /// </summary>
        public Hex Hex {
            get { return hex; }
            set { hex = value; }
        }

        private bool outlined;
        /// <summary>
        /// Gets or sets whether the hex should be drawn with a colored outline.
        /// </summary>
        public bool Outlined {
            get { return outlined; }
            set { outlined = value; }
        }

        private Color outlineColor;

        /// <summary>
        /// The color to use when drawing the outline (if Outlined is true).
        /// </summary>
        public Color OutlineColor {
            get { return outlineColor; }
            set { outlineColor = value; }
        }

        private bool shadowed;
        /// <summary>
        /// Gets or sets whether the hex should be drawn with a shadow over it.
        /// </summary>
        public bool Shadowed {
            get { return shadowed; }
            set { shadowed = value; }
        }

        /// <summary>
        /// Gets a Color to use to display the Hex, based on the Hex's TerrainType.
        /// </summary>
        public Color TerrainColor {
            get {
                switch (hex.Terrain) {
                    case Hex.TerrainType.Field: return Color.PaleGoldenrod;
                    case Hex.TerrainType.Forest: return Color.ForestGreen;
                    case Hex.TerrainType.Grass: return Color.LawnGreen;
                    case Hex.TerrainType.Hills: return Color.SaddleBrown;
                    case Hex.TerrainType.Mountain: return Color.SlateGray;
                    case Hex.TerrainType.Swamp: return Color.Olive;
                    case Hex.TerrainType.Tundra: return Color.Snow;
                    case Hex.TerrainType.Water: return Color.CornflowerBlue;
                    default: return Color.Red;
                }
            }
        }
        /// <summary>
        /// Constructs a new HexDisplay object with scale 1 and with the specified sprite.
        /// </summary>
        /// <param name="hexToDisplay"></param>
        /// <param name="x">The x coordinate of the origin of the Hex.</param>
        /// <param name="y">The y coordinate of the origin of the Hex.</param>
        /// <param name="hexSprite">The Texture2D used to draw the Hex.</param>
        public HexDisplay(Hex hexToDisplay, float x, float y, Texture2D hexSprite, Texture2D outline, Texture2D shadow)
            :
            this(hexToDisplay, x, y, 1, hexSprite, outline, shadow) { }
        /// <summary>
        /// Constructs a new HexDisplay.
        /// </summary>
        /// <param name="hexToDisplay">The Hex that this HexDisplay displays.</param>
        /// <param name="x">The x coordinate of the origin of the Hex.</param>
        /// <param name="y">The y coordinate of the origin of the Hex.</param>
        /// <param name="scale">The factor by which to scale the Hex.</param>
        /// <param name="hexSprite">The Texture2D used to draw the Hex.</param>
        public HexDisplay(Hex hexToDisplay, float x, float y, float scale, Texture2D hexSprite, Texture2D outline, Texture2D shadow)
            :
            base(x, y, scale, hexSprite) {
            this.hex = hexToDisplay;
            this.hexOutline = outline;
            this.hexShadow = shadow;
        }

        public override void Draw(SpriteBatch spriteBatch) {
            spriteBatch.Draw(Sprite, Bounds, this.TerrainColor);
            if (shadowed) spriteBatch.Draw(hexShadow, Bounds, Color.White);
            if (outlined) spriteBatch.Draw(hexOutline, Bounds, outlineColor);
        }

        public override string ToString() {
            return "HexDisplay for " + hex + "--origin: " + Origin;
        }
    }
}
